package com.mzmarket.market.app.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mzmarket.market.app.domain.SignVO;
import com.mzmarket.market.app.domain.ValueInfo;
import com.mzmarket.market.module.entity.User;
import com.mzmarket.market.module.service.UserService;
import com.mzmarket.market.module.utils.JwtCookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Base64;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/login")
    public void login(
            @RequestParam String phone,
            @RequestParam String password,
            HttpServletResponse res
    ) throws IOException {
        res.setContentType("text/plain;charset=UTF-8");
        User user = null;

        try {
            user = userService.getByPhone(phone);
        } catch (Exception e) {
            res.getWriter().write("手机号输入错误");
        }

        if (user == null) {//判断用户是否存在
            res.getWriter().write("用户不存在");
        } else {
            //判断密码是否匹配
            if (user.getPassword().equals(DigestUtils.md5DigestAsHex((password + user.getSalt()).getBytes()))) {
                /*把信息通过Json返回*/
                //使用JWT加密
                String json = getString(phone);
                res.getWriter().write(json);
            } else {
                res.getWriter().write("密码不正确");
            }
        }
    }

    @GetMapping("/register")
    public void register(
            @RequestParam String phone,
            @RequestParam String password,
            @RequestParam String name,
            @RequestParam String avatar,
            HttpServletResponse res) throws IOException {
        res.setContentType("text/plain;charset=UTF-8");
        User user = null;
        try {
            user = userService.getByPhone(phone);
        } catch (Exception e) {
            res.getWriter().write("手机号输入格式错误");
        }

        if (user == null) {//判断用户是否存在
            userService.insert(phone, password, name, avatar);
            String json = getString(phone);
            res.getWriter().write(json);
        } else {
            res.getWriter().write("用户已存在");
        }
    }

    private static String getString(String phone) throws JsonProcessingException {
        String token = JwtCookieUtil.encrypt(phone);

        //将加密好的token封装
        ValueInfo valueInfo = new ValueInfo().setToken(token);

        // 将封装好的token对象序列化,并使用base64编码
        ObjectMapper objectMapper = new ObjectMapper();
        String sign = Base64.getEncoder().encodeToString(objectMapper.writeValueAsString(valueInfo).getBytes());

        SignVO signVO = new SignVO().setSign(sign);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(signVO);
    }

}

